// pages/Settlement/OrderDetails/OrderDetails.js
const controllers = require('../../controllers/controllerIndex.js').controller;
const controller=require('../../controllers/controllerOrder').controller
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index02:-1,
    index03:-1,
    array02:['2020-10-29 08:00:00','2020-10-29 18:00:00'],
    shopAccountId:''
  },
  choicemaster(e){
    this.setData({
      index03:e.detail.value,
      choicemaster:e.detail.value,
      shopAccountId:this.data.masterid[e.detail.value]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     let that =this
    controllers.orderDetail({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      offlineOrderId:  options.id||'',
      orderNo:  options.orderNo||''
    }).then(res => {
      let hasService =false
      var ids=[]
      var goods=res.data.offlineOrderGoodsList;
      goods.forEach(function(item,index){
        ids.push(item.id)
        if(item.type==0){
          hasService=true
        }
      })
      that.setData({
        info: res.data,
        id:options.id,
        hasService
      })
      if(that.data.info.accountId)
      that.getCouponList(that.data.info.accountId,ids)
    })
    //获取技师列表
    controller.getMaster({
      shopId:wx.getStorageSync('shopId')
    }).then(res =>{
      let arr=[]
      let masterid=[]
      res.data.forEach(function(item,index){
        // index02.push(item.nick)
        // masterid.push(item.id)
        arr.push(item.nick)
        masterid.push(item.id)
      })
      this.setData({
        array03:arr,
        masterid:masterid
      })
    })
   

    
  },
  addCoupon(){
    if(this.data.couponList)
    this.setData({
      isShow:true
    })
    else
    wx.showToast({
      title: '暂无优惠券可选',
    })
  },
  getCouponList(id,goodsIds){
    controllers.canUseCoupon({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      goodsIds:goodsIds,
      accountId:id,
      totalPrice:this.data.info.money
    }).then(res =>{
    
     this.setData({
      couponList:res.data
     })
    })
  },

  hideAddCoupon(){
    this.setData({
      isShow:false
    })
  },
    //确认选择
    determine(){
      let addCouponList
      addCouponList = this.data.couponList.filter(item=>item.select)
      this.setData({
        isShow:false
      })
      if(addCouponList.length>0){
        this.setData({
          addCouponList
        })
      }
    },
    //选择
    select(e){
      let index = e.currentTarget.dataset['index'];
      let couponList = this.data.couponList
      couponList[index].select = !couponList[index].select
      this.setData({
        couponList
      })
    },
  descinput(e){
    
    
    this.setData({
      limit:e.detail.dataset.length,
      inputvalue:e.detail.dataset
    })

    if(this.data.limit >=100){
      this.setData({
        caninput:true
      })
    }else{
      this.setData({
        caninput:false
      })
    }
  },
  check(e){
  this.setData({payType:e.currentTarget.dataset.id})
  },
  choicetime1(e){
    let date=e.detail.value
    // this.setData({
    //   index02:e.detail.value
    // })
    this.setData({
      date:date
    })
    console.log(e);
  },
  choicetime2(e){
    let time=e.detail.value
    // this.setData({
    //   index02:e.detail.value
    // })
    this.setData({
      time:time
    })
    console.log(e);
  },
  confrim(){
    let that =this
    let seletCoupon='';
    wx.showModal({
      title: '提示',
      content: '是否结算？',
      success (res) {
        if (res.confirm) {
          if(that.data.addCouponList){
            seletCoupon=that.data.addCouponList[0].id
            debugger
          }
          let json={
            
            sessionId	: wx.getStorageSync('token'),
            shopId: wx.getStorageSync('shopId'),
            offlineOrderId:that.data.id,
          
           
            
            // agreeStartTime: "",
            // agreeEndTime :that.data.array03[that.data.index03]
          }
          json.agreeTime=that.data.date+" "+that.data.time
          if(that.data.shopAccountId||that.data.info.shopAccountId){
            json.shopAccountId=that.data.shopAccountId||that.data.info.shopAccountId
          }
          if(that.data.inputvalue){
            json.desc=that.data.inputvalue
          }
          if(seletCoupon){
            json.couponAccountId=seletCoupon
          }
          if(that.data.payType){
                      json.payType =that.data.payType
          }else{wx.showToast({
                  title: '请选择支付方式',
                })
                            return
          }
             
      
          controllers.settlementOrder(json).then(res => {
             if(res.code==1){
             
               wx.navigateBack({
                 complete: (res) => { wx.showToast({
                  title: '结算成功',
                })},
               }) 
             }
      
        })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
},
  cancel() {
    let id = this.data.id;
    controllers.cancelOrder({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      offlineOrderId: id
    }).then(res => {
       if(res.code==1){
       
         wx.navigateBack({
           complete: (res) => { wx.showToast({
            title: '取消成功',
          })},
         }) 
       }

    })
  },
  edit() {
    wx.navigateTo({
      url: '../EditOrder/EditOrder',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})